#pragma once
#include <iostream>
#include <sys/epoll.h>
#include <unistd.h>
#include "Log.hpp"

using namespace std;

class Epoll
{
public:
    static const int gsize = 256; // epoll_create的无用参数
public:
    static int EpollCreate()
    {
        int epfd = epoll_create(gsize);
        if (epfd < 0)
        {
            logMessage(ERROR, "create epoll fail!\n");
            exit(1);
        }
        return epfd;
    }
    static bool EpollCtl(int epfd, int op, int fd, uint32_t events) // 对epfd这个epoll模型做op操作(增删改)，注册fd的events为关心事件
    {
        // 1.构建节点
        struct epoll_event ev;
        ev.data.fd = fd;
        ev.events = events;
        // 2.注册事件
        int n = epoll_ctl(epfd, op, fd, &ev);
        if (n > 0)
        {
            logMessage(ERROR, "add listensock to epoll fail!\n");
            exit(2);
        }
        return true;
    }
    static int EpollWait(int epfd, struct epoll_event *revs /*事件数组*/, int maxevents /*数组长度*/, int timeout /*等待策略*/)
    {
        int n = epoll_wait(epfd, revs, maxevents, timeout);
        if (n < 0)
        {
            logMessage(ERROR, "epoll wait fail!\n");
            exit(3);
        }
        return n; // 就绪事件个数
    }
};